// UniversalAdmin.Infrastructure/Data/Seed/DataSeeder.cs
using UniversalAdmin.Domain.Entities;
using Microsoft.EntityFrameworkCore;

namespace UniversalAdmin.Infrastructure.Data.Seed;

public static class DataSeeder
{
  public static async Task SeedAsync(UniversalAdminDbContext context)
  {
    await SeedRoles(context);
    await SeedMenus(context);
    await SeedUsers(context);
    await SeedUserRoles(context);
    await SeedSystemConfigs(context);
    await SeedArticleCategories(context);
  }

  private static async Task SeedArticleCategories(UniversalAdminDbContext context)
  {
    if (await context.ArticleCategories.AnyAsync()) return;

    var categories = new List<ArticleCategory>
    {
        ArticleCategory.Create("通用后台", "关于通用后台的介绍", parentId: null, sort: 1),
        ArticleCategory.Create("技术分享", "技术相关文章", parentId: null, sort: 2),
        ArticleCategory.Create("系统公告", "系统更新与公告", parentId: null, sort: 3)
    };
    await context.ArticleCategories.AddRangeAsync(categories);
    await context.SaveChangesAsync();
  }

  private static async Task SeedRoles(UniversalAdminDbContext context)
  {
    if (await context.Roles.AnyAsync()) return;

    var roles = new List<Role>
    {
      Role.Create("SuperAdmin", "超级管理员，拥有所有权限"),
      Role.Create("Admin", "管理员，拥有大部分管理权限"),
      Role.Create("User", "普通用户，只有基本权限")
    };

    await context.Roles.AddRangeAsync(roles);
    await context.SaveChangesAsync();
  }

  private static async Task SeedMenus(UniversalAdminDbContext context)
  {
    if (await context.Menus.AnyAsync()) return;

    // 用工厂方法创建
    var menus = new List<Menu>
    {
        Menu.Create("仪表盘",  "/dashboard", "Dashboard", parentId: null, sort: 1),
        Menu.Create("用户管理", "/users",       "User",      parentId: null, sort: 2),
        Menu.Create("用户列表", "/users/list",  "List",      parentId: 2,    sort: 1),
        Menu.Create("添加用户", "/users/add",   "Plus",      parentId: 2,    sort: 2)
    };

    await context.Menus.AddRangeAsync(menus);
    await context.SaveChangesAsync();
  }

  private static async Task SeedUsers(UniversalAdminDbContext context)
  {
    if (await context.Users.AnyAsync()) return;

    var adminUser = new User
    {
      Id = 1,
      Username = "admin",
      Password = "$2a$12$YlXJlEk3a36wHokcwJdX4uoVeW19JuTedL3V2aFciMcEvEV2OvDjG", // admin123
      Email = "admin@example.com",
      IsActive = true
    };

    await context.Users.AddAsync(adminUser);
    await context.SaveChangesAsync();
  }

  private static async Task SeedUserRoles(UniversalAdminDbContext context)
  {
    if (await context.UserRoles.AnyAsync()) return;

    var userRoles = new List<UserRole>
        {
            new() { UserId = 1, RoleId = 1 } // admin用户拥有SuperAdmin角色
        };

    await context.UserRoles.AddRangeAsync(userRoles);
    await context.SaveChangesAsync();
  }

  private static async Task SeedSystemConfigs(UniversalAdminDbContext context)
  {
    var requiredConfigs = new List<(string Key, string Value, string Description)>
    {
        ("site_name", "通用后台管理系统", "网站名称"),
        ("site_title", "通用后台管理系统 - 高效、安全、易用", "网站标题"),
        ("site_description", "一个基于Vue3和.NET8构建的现代化后台管理系统", "网站描述"),
        ("site_keywords", "后台管理,Vue3,.NET8,权限管理", "网站关键词"),
        ("site_logo", "", "网站Logo"),
        ("copyright", "© 2024 通用后台管理系统. All rights reserved.", "版权信息")
    };

    foreach (var (key, value, description) in requiredConfigs)
    {
      var existingConfig = await context.SystemConfigs.FirstOrDefaultAsync(c => c.Key == key);
      if (existingConfig == null)
      {
        var config = new SystemConfig 
        { 
          Key = key, 
          Value = value, 
          Description = description,
          CreatedAt = DateTime.UtcNow,
          UpdatedAt = DateTime.UtcNow
        };
        await context.SystemConfigs.AddAsync(config);
      }
    }

    await context.SaveChangesAsync();
  }
}